package com.rainbow.common.handler.config;

import com.google.common.collect.Maps;
import com.rainbow.common.handler.filter.TraceFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import javax.servlet.DispatcherType;
import java.util.Map;

/**
 * 配置过滤器
 * @author 傅为地
 */
@Configuration
public class GlobalFilterConfig {

    /**
     * 全局trace过滤器
     */
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public FilterRegistrationBean<TraceFilter> traceFilterRegistration(){
        FilterRegistrationBean<TraceFilter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST,DispatcherType.FORWARD);
        registration.setFilter(new TraceFilter());
        registration.addUrlPatterns("/*");
        //不加traceId的地址(nacos地址,actuator地址)
        Map<String,String> initMap= Maps.newHashMap();
        initMap.put("noTraceUrls","/v1/cs/,/actuator/");
        registration.setInitParameters(initMap);
        registration.setName("traceFilter");
        registration.setOrder(Integer.MAX_VALUE);
        return registration;
    }

}
